package com.controller;

import com.entity.*;
import com.service.DoctorService;
import com.service.PatientService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

@WebServlet(name = "PatientServlet", value = "/patientServlet")
public class PatientServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        HttpSession session = request.getSession(false);

        if(session == null){
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

        String operation = request.getParameter("operation");

//        System.out.printf(operation);


        /**通过账号获取病人的个人信息**/
        String patientAccount = (String) session.getAttribute("paccount");
        PatientService patientService = new PatientService();
        Patient patient = patientService.findThisPatient(patientAccount);

        DoctorService doctorService = new DoctorService();

        request.setAttribute("patientInfo",patient);

//        int result = 0;



        /**信息不完善先完善信息**/
        if(!operation.equals("commitInfo")){
            if (!patientService.isComplete(patientAccount))
                operation = "addInfo";
        }
        //System.out.printf(":::::"+operation+":::::");

        switch (operation) {
            case "homePage":    //主页
                request.getRequestDispatcher("patient.jsp").forward(request,response);
                break;
            case "register":
                Vector<RegisterDoc> registerDocs;
                registerDocs = patientService.thisPatientRegisterDocs(patientAccount);
                request.setAttribute("patientRegisterDocs",registerDocs);
                request.getRequestDispatcher("register.jsp").forward(request,response);     //挂号页面,显示挂号记录
                break;
            case "addRegister":
                request.getRequestDispatcher("addRegister.jsp").forward(request,response);     //申请挂号页面

                break;

            case "addInfo":     //完善个人信息页面
                request.getRequestDispatcher("addInfo.jsp").forward(request,response);
                break;

            case "commit":      //提交挂号单
                /**获取当前时间**/
                Date date = new Date();

                String addDoctorName = request.getParameter("doctor");//获取所挂号的医生的工号
                Doctor doctor = doctorService.findThisDoctorByName(addDoctorName);
                RegisterDoc registerDoc = new RegisterDoc(patient.getName(),patient.getSexNum(),date,doctor.getCost(),doctor.getDepartment()
                        ,doctor.getWorkId(),doctor.getName(),patient.getAccount());
                int addResult = patientService.addRegister(registerDoc);
                if(addResult>0)
                    request.getRequestDispatcher("patient.jsp").forward(request,response);
                else
                    request.getRequestDispatcher("addRegister.jsp").forward(request,response);
                break;

            case "commitInfo":    //提交个人信息完善
                /**获取当前时间**/
                System.out.println("AAAAAAA");
                SimpleDateFormat sdf = new SimpleDateFormat();// 格式化时间
                sdf.applyPattern("yyyy-MM-dd HH:mm:ss");// a为am/pm的标记
                Date date1 = new Date();
                String rightNowDate = sdf.format(date1);
                int thisYear = Integer.parseInt(rightNowDate.substring(0, 4));

                /**获取生日**/
                String bornDate = request.getParameter("comBornDate");
                int bornYear = Integer.parseInt(bornDate.substring(0,4));
                int bornMonth = Integer.parseInt(bornDate.substring(5, 7));
                int bornDay = Integer.parseInt(bornDate.substring(8, 10));

                String comName = request.getParameter("comName");
                String comIdentityId = request.getParameter("comIdentityId");
                String comSex = request.getParameter("comSex");
                int comSexNum = patient.sexNum(comSex);
                String comPhone = request.getParameter("comPhone");
                String comMICN = request.getParameter("comMICN");
                Calendar calendar =Calendar.getInstance();
                calendar.set(bornYear,bornMonth-1,bornDay);
                Date comBornDate = calendar.getTime();
                int comAge = thisYear - bornYear;
                //System.out.println(thisYear);
                //System.out.println(" "+bornYear);
                //System.out.println(comIdentityId+" "+comSex+" "+comSexNum+" "+comPhone+" "+comMICN+" "+comAge+" "+comBornDate+" "+comName);
                Patient comPatient = new Patient(comName,comIdentityId,patient.getPatientPwd(),comSexNum,patientAccount
                        ,comPhone,comAge,comMICN,comBornDate);
                patientService.updateThisPatientInfo(comPatient);

                request.getRequestDispatcher("login.jsp").forward(request,response);
                //response.sendRedirect("patientServlet?operation=homePage");
                break;

            case "drugRecord":            //查看取药记录
                Vector<Drug> drugs = patientService.thisPatientDrugs(patientAccount);
                request.setAttribute("patientDrugs",drugs);
                request.getRequestDispatcher("drugRecord.jsp").forward(request,response);
                break;

            case "diagnoseRecord":
                Vector<Diagnose> diagnoses = patientService.thisPatientDiagnoses(patientAccount);
                request.setAttribute("patientDiagnoses",diagnoses);
                request.getRequestDispatcher("diagnoseRecord.jsp").forward(request,response);             //查看诊断记录
                break;

            case"costRecord":             //查看缴费记录
                Vector<Cost> costs = patientService.thisPatientCosts(patientAccount);
                request.setAttribute("patientCosts",costs);
                request.getRequestDispatcher("costRecord.jsp").forward(request,response);
                break;

            case "exit":
                request.getRequestDispatcher("login.jsp").forward(request,response);
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }
}
